package com.xiansi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiansi.mapper.PlayerMapper;
import com.xiansi.mapper.TeamMapper;
import com.xiansi.pojo.Player;
import com.xiansi.pojo.PlayerExample;
import com.xiansi.pojo.Team;
import com.xiansi.pojo.TeamExample;
import com.xiansi.vo.QueryPlayerVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理球员的业务逻辑层
 */
@Service
public class PlayerService {
    @Resource
    private PlayerMapper playerMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private TeamService teamService;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Player> queryByPageInfo(Integer pageNum, Integer pageSize, QueryPlayerVO vo) {
        //多条件
        PlayerExample playerExample = new PlayerExample();
        TeamExample teamExample = new TeamExample();

        //创建条件容器
        PlayerExample.Criteria playerExampleCriteria = playerExample.createCriteria();
        TeamExample.Criteria teamExampleCriteria = teamExample.createCriteria();

        if (vo != null) {

            if (vo.getTeamName() != null && !"".equals(vo.getTeamName())) {
                teamExampleCriteria.andTeamNameLike("%" + vo.getTeamName() + "%");
                List<Team> list = teamMapper.selectByExample(teamExample);
                List<Integer> list1 = new ArrayList<>();
                for (Team team : list) {
                    Integer teamId = team.getTeamId();
                    list1.add(teamId);
                }
                playerExampleCriteria.andTeamIdIn(list1);
            }
            if (vo.getPlayerName() != null && !"".equals(vo.getPlayerName())) {
                playerExampleCriteria.andPlayerNameLike("%" + vo.getPlayerName() + "%");
            }
            if (vo.getPlayerNum() != null && !"".equals(vo.getPlayerNum())) {
                playerExampleCriteria.andPlayerNumEqualTo(vo.getPlayerNum());
            }
            if (vo.getLocation() != null && !"".equals(vo.getLocation())) {
                playerExampleCriteria.andLocationEqualTo(vo.getLocation());
            }
            if (vo.getBirthday() != null && !"".equals(vo.getBirthday())) {
                playerExampleCriteria.andBirthdayEqualTo(vo.getBirthday());
            }
            if (vo.getNationality() != null && !"".equals(vo.getNationality())) {
                playerExampleCriteria.andNationalityEqualTo(vo.getNationality());
            }
            if (vo.getHeight() != null && !"".equals(vo.getHeight())) {
                playerExampleCriteria.andHeightGreaterThan(vo.getHeight());
            }
            if (vo.getWeight() != null && !"".equals(vo.getWeight())) {
                playerExampleCriteria.andWeightGreaterThanOrEqualTo(vo.getWeight());
            }
            if (vo.getSalary() != null && !"".equals(vo.getSalary())) {
                playerExampleCriteria.andSalaryGreaterThanOrEqualTo(vo.getSalary());
            }
            if (vo.getStatus() != -1) {
                playerExampleCriteria.andStatusEqualTo(vo.getStatus());
            }
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<Player> players = playerMapper.selectByExample(playerExample);
        for (Player player : players) {
            Team team = teamService.queryById(player.getTeamId());
            player.setTeam(team);
        }
        return new PageInfo<>(players);
    }

    /**
     * 新增球员
     *
     * @param player
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer addPlayer(Player player) {
        return playerMapper.insertSelective(player);
    }

    /**
     * 根据id查询球员信息
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Player queryById(Integer id) {
        return playerMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新球员信息
     *
     * @param player
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer updatePlayer(Player player) {
        return playerMapper.updateByPrimaryKeySelective(player);
    }

    /**
     * 根据id删除球员
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer deletePlayer(Integer id) {
        return playerMapper.deleteByPrimaryKey(id);
    }


}
